package com.baizhi.springbootshiro.controller;

import com.baizhi.springbootshiro.entity.User;
import com.baizhi.springbootshiro.service.PermsService;
import com.baizhi.springbootshiro.service.UserService;
import com.google.code.kaptcha.Producer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.subject.Subject;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

/**
 * @author Administrator
 */
@Controller
@Api(tags = "UserController", value = "描述", description = "过期描述", protocols = "http")
public class UserController {
    @Resource
    private Producer producer;
    @Resource
    private UserService userService;
    @Resource
    private PermsService permsService;

    @ApiOperation("登录")
    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/")
    public String index() {
        return "login";
    }

    @ApiOperation("首页·")
    @GetMapping("/index")
    public String success() {
        return "index";
    }

    @ApiOperation("首页·")
    @GetMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/";
    }


    @ApiOperation("submit·")
    @PostMapping("/submit")
    public String submit(String uname, String pwd) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(uname, pwd);
        try {
            System.out.println("subject:" + subject);
            subject.login(token);
            return "redirect:/index";
        } catch (UnknownAccountException e) {
            System.out.println("用户名错误");
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码错误");
        }
        return "redirect:/login";
    }


    @GetMapping("/captcha")
    public void captcha(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String random = RandomStringUtils.randomAlphanumeric(4);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        request.setAttribute(uuid, random);
        request.setAttribute("uuid", uuid);
        BufferedImage image = producer.createImage(random);
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(image, "jpg", outputStream);
        IOUtils.closeQuietly(outputStream);
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    /**
     * 注册提交位置
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/register/submit")
    public String regSub(User user, HttpServletRequest request) {
        String s = request.getAttribute(user.getUuid()) + "";
        boolean save = userService.save(user);
        if (save) {
            return "redirect:/login";
        } else {
            return "redirect:/register";
        }
    }
}
